package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.eneity.SkuInfo;
import com.atguigu.gmall.product.eneity.SpuImage;
import com.atguigu.gmall.product.eneity.SpuSaleAttr;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.vo.SaveSkuInfoVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "获取sku分页列表")
@RestController
@RequestMapping("admin/product")
public class SkuInfoController {

    @Autowired
    SkuInfoService skuInfoService;

    @Autowired
    SpuImageService spuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    @ApiOperation("获取sku分页列表")
    @GetMapping("list/{page}/{limit}")
    public Result getSkuInfoPage(@PathVariable("page") Long page , @PathVariable("limit") Long limit){

        Page<SkuInfo> page1 = skuInfoService.lambdaQuery().page(new Page<>(page, limit));
        return Result.ok(page1);
    }

    @ApiOperation("根据spuId获取图片列表")
    @GetMapping("spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId){

        List<SpuImage> list = spuImageService.lambdaQuery().eq(SpuImage::getSpuId, spuId).list();
        return Result.ok(list);
    }

    @ApiOperation("根据spuId获取销售属性值和名")
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttr(@PathVariable Long spuId){

        List<SpuSaleAttr> list = spuSaleAttrService.getSpuSaleAttrAndValue(spuId);
        return Result.ok(list);
    }

    @ApiOperation("添加一个SkuInfo")
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SaveSkuInfoVo saveSkuInfoVo){

        skuInfoService.saveSkuInfo(saveSkuInfoVo);
        return Result.ok();
    }
}
